<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/03/11
 */
namespace api\modules\v1\controllers;

use common\logic\Boat;
use common\logic\Fund;
use common\logic\Occupancy;
use common\logic\Trip;
use common\logic\User;
use common\models\Booker;
use common\models\Order;
use common\models\Cabin;
use common\tool\FormField;
use yii\web\Controller;
use Yii;
use yii\web\Response;

class BoatController extends Controller {
    const DEBUG = 0;
    public function beforeAction($action) {
        Yii::$app->response->format = Response::FORMAT_JSON;
        self::DEBUG && Yii::info($_SERVER['REQUEST_URI'],__METHOD__);
        return parent::beforeAction($action);
    }
    public function afterAction($action, $result) {
        self::DEBUG && Yii::info($result,__METHOD__);
        return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
    }

    public function actionInfo() {
        $boatId = Yii::$app->request->get('boatId');
        if (empty($boatId)) {
            return [
                'code' => 401,
                'msg' => '参数错误'
            ];
        }
        $res = Boat::getInfo($boatId);
        if ($res['err'] == 0) {
            return [
                'code' => 200,
                'data' => $res['data']
            ];
        } elseif ($res['err'] == 1) {
            return [
                'code' => 404,
                'msg' => '船宿不存在'
            ];
        } else {
            return [
                'code' => 409,
                'msg' => '未知错误'
            ];
        }
    }

    public function actionTrip() {
        $cursor = Yii::$app->request->get('cursor', 0);
        $limit = Yii::$app->request->get('limit', 10);
        $startDate = Yii::$app->request->get('startDate');
        $endDate = Yii::$app->request->get('endDate');
        $boatId = Yii::$app->request->get('boatId');
        $tripId = Yii::$app->request->get('tripId');
        $data = [];
        if ($boatId) {
            $data = Trip::getTrips($boatId, $startDate, $endDate, $cursor, $limit);
        } elseif ($tripId) {
            $data = Trip::getTrip($tripId);
        }
        $res = [
            'code' => 200,
            'data' => $data
        ];
        return $res;
    }

    public function actionCabin() {
        $tripId = Yii::$app->request->get('tripId');
        $cabinData = Trip::getCabinsByTripId($tripId);
        return [
            'code' => 200,
            'data' => [
                'list' => $cabinData,
            ],
        ];
    }

    public function actionCoupon() {
        if (Yii::$app->user->isGuest) {
            return [
                'code' => 403,
                'msg' => '未登录'
            ];
        }
        /** @var \api\models\User $user */
        $user = Yii::$app->user->getIdentity();
        return [
            'code' => 200,
            'data' => [
                'fundAmount' => $user->fund_amount,
                'fundRatio' => Yii::$app->params['fundRatio'],
            ]
        ];
    }

    public function actionTest() {
        $bookerBoatId = Yii::$app->request->get('bookerBoatId', 290);
        $diveBoatId = Booker::booker2dive($bookerBoatId, Booker::TYPE_BOAT);
        $boat=\common\models\Boat::getOne("id=$diveBoatId");
        $startMonth = date('Y-m');
        while (true) {
            $trip = \common\models\Trip::getOne("boat_id=$diveBoatId AND start_time LIKE '$startMonth%'");
            $tmpId=Booker::booker2dive(32694,Booker::TYPE_TRIP);
            //$tmpId=1832;
            $trip=\common\models\Trip::getOne("id=$tmpId");
            if ($trip) {
                break;
            }
            $startMonth = date('Y-m', strtotime('+1 month', strtotime($startMonth)));
        }
        //$trip = \common\models\Trip::getOne("id=26");
        $occupancies = Occupancy::getOccupancies($trip['id']);
        $oc = [];
        foreach ($occupancies as $v) {
            $cabin = Cabin::getOne("id={$v['cabin_id']}");
            $oc[] = [
                'occupancyId' => $v['id'],
                'cabinId' => $cabin['id'],
                'cabinNum' => $cabin['cabin_number'],
                //'Max'=>$cabin['occupancy'],
                //'freeSpaces'=>$v['free_spaces'],
                'name_en' => $v['name_en'],
                'persons_max' => $v['persons_max'],
                'persons_current' => $v['persons_current'],
                'price' => $v['price'],
            ];
        }
        return [
            'boat'=>[
                'id'=>$boat['id'],'booker_url'=>$boat['booker_url'],'bookerId'=>$bookerBoatId,
            ],
            'trip' => [
                'tripId' => $trip['id'],
                'bookerTripId'=>Booker::dive2booker($trip['id'],Booker::TYPE_TRIP),
                'freeSpaces' => $trip['free_spaces'],
            ],
            'occupancy' => $oc,
        ];
    }
    public function actionCabinOccupancy(){
        $o = 0;
        $l = 100;
        $r = [];
        while ($list = \common\models\Occupancy::getDb()->createCommand("SELECT a.id,b.id as cabinId,a.name_en,b.bedding FROM `dive_occupancy` as a join dive_cabin as b on a.cabin_id=b.id limit $o,$l")->queryAll()) {
            foreach ($list as $k => $v) {
                $r[$v['bedding']][$v['name_en']] = true;
//                if($v['bedding']=="Double bed"&&$v['name_en']=="Twin Share"){
//                    return $v;
//                }
            }
            $o += $l;
        }
        foreach ($r as $k=>$v){
            $r[$k]=array_keys($v);
        }
        return $r;
    }
    public function actionT(){
        $r=User::useFundAmount(1,100);
        var_dump($r);exit;

        $o=0;$l=500;
        $res=[];
        while ($r=Yii::$app->db->createCommand("select * from dive_occupancy limit $o,$l")->queryAll()){
            foreach ($r as $item) {
                $res[$item['trip_id'].'-'.$item['cabin_id']][] = $item['name_en'];
                if(in_array('Triple Share',$res[$item['trip_id'].'-'.$item['cabin_id']]) && in_array('Twin Share',$res[$item['trip_id'].'-'.$item['cabin_id']])){
                    var_dump($res[$item['trip_id'].'-'.$item['cabin_id']]);
                    var_dump($item['trip_id']);
                }
            }
            $o+=$l;
        }
        exit;
        $a='Twin_beds-->Twin_Share
Twin_beds-->Single_Occupancy
Twin_beds-->Double_Occupancy
Double_bed-->Double_Occupancy
Double_bed-->Twin_Share
Double_bed-->Single_Occupancy
Double_bed-->Triple_Occupancy
3_Twin_beds-->Triple_Share
3_Twin_beds-->Triple_Occupancy
3_Twin_beds-->Double_Occupancy
3_Twin_beds-->Single_Occupancy
Double_bed_+_Twin_bed__bunk_style_-->Twin_Share
Double_bed_+_Twin_bed__bunk_style_-->Single_Occupancy
Double_bed_+_Twin_bed__bunk_style_-->Double_Occupancy
Double_bed_+_Twin_bed__bunk_style_-->Triple_Occupancy
Double_bed_+_Twin_bed__bunk_style_-->Quad_Share
Double_bed_+_Twin_bed__bunk_style_-->Triple_Share
Double_bed_+_Twin_bed__bunk_style_-->Quad_Occupancy
Twin_beds__bunk_style_-->Twin_Share
Twin_beds__bunk_style_-->Single_Occupancy
Twin_beds__bunk_style_-->Double_Occupancy
2_Twin_beds_+_2_Twin_beds__bunk_style_-->Quad_Share
2_Twin_beds_+_2_Twin_beds__bunk_style_-->Triple_Occupancy
2_Twin_beds_+_2_Twin_beds__bunk_style_-->Quad_Occupancy
2_Twin_beds_+_2_Twin_beds__bunk_style_-->Twin_Share
2_Twin_beds_+_2_Twin_beds__bunk_style_-->Single_Occupancy
Double_bed_+_Twin_bed-->Double_Occupancy
Double_bed_+_Twin_bed-->Twin_Share
Double_bed_+_Twin_bed-->Single_Occupancy
Double_bed_+_Twin_bed-->Triple_Occupancy
Double_bed_+_2_Twin_beds__bunk_style_-->Triple_Share
Double_bed_+_2_Twin_beds__bunk_style_-->Twin_Share
2_Twin_beds_+_1_Twin_bed__bunk_style_-->Triple_Share
2_Twin_beds_+_1_Twin_bed__bunk_style_-->Triple_Occupancy
2_Twin_beds_+_1_Twin_bed__bunk_style_-->Twin_Share
2_Twin_beds_+_1_Twin_bed__bunk_style_-->Single_Occupancy
2_Twin_beds_+_1_Twin_bed__bunk_style_-->Double_Occupancy
Single_bed-->Single_Occupancy
Single_bed-->Twin_Share
Double_bed_+_2_Twin_beds-->Double_Occupancy
Double_bed_+_2_Twin_beds-->Twin_Share
Double_bed_+_2_Twin_beds-->Triple_Share
Double_bed_+_2_Twin_beds-->Triple_Occupancy
Double_bed_+_2_Twin_beds-->Quad_Share
Double_bed_+_2_Twin_beds-->Quad_Occupancy
2_Double_beds_+_2_Twin_beds__bunk_style_-->Quad_Share
2_Double_beds_+_2_Twin_beds__bunk_style_-->Twin_Share
2_Double_beds_+_2_Twin_beds__bunk_style_-->Single_Occupancy
Double_bed_or_Twin_bed-->Double_Occupancy
Double_bed_or_Twin_bed-->Single_Occupancy
Double_bed_or_Twin_bed-->Twin_Share
Twin_/_Double__convirtable_-->Twin_Share
Twin_/_Double__convirtable_-->Double_Occupancy
Twin_/_Double__convirtable_-->Single_Occupancy
Double_bed_+_Double_bed__bunk_style_-->Double_Occupancy
Double_bed_+_Double_bed__bunk_style_-->Twin_Share
Double_bed_+_Double_bed__bunk_style_-->Single_Occupancy';
        $a=FormField::rowData($a,'-->');
        $r=[];
        foreach ($a as $item){
            $r[$item[0]][] = $item[1];
        }
        foreach ($r as $k=>$item){
            sort($r[$k]);
            $r[$k]=implode('__',$r[$k]);
        }
        $r=array_unique($r);
        foreach ($r as $k=>$item){
            $r[$k]=explode('__',$r[$k]);
        }
        return array_values($r);
    }
}
